﻿using System;
using System.Collections.Generic;

using Fuse8.DomainFramework.Domain;
using Fuse8.DomainFramework.Common;
using Fuse8.DomainFramework.Common.Locator;

namespace Fuse8.DomainFramework.DataAccess.SqlDatabase
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <typeparam name="TKey"></typeparam>
    public static class SqlEntityDataRepositoryConfiguration<TEntity, TKey>
        where TEntity : class, IEntityObject<TKey>, new()
        where TKey : IComparable<TKey>, IEquatable<TKey> 
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        public static IUnitOfWorkFactory<TEntity> CreateUnitOfWorkFactory(string connectionString)
        {
            Guard.ArgumentNotEmpty(connectionString, "connectionString");

            return new HttpContextSqlUnitOfWorkFactory<TEntity>(connectionString);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static SqlEntityDataRepository<TEntity, TKey> CreateRepository()
        {
            if (!DependencyServiceLocator.Current.IsDefaultServiceRegistered<ISqlEntityDataMapper<TEntity, TKey>>())
            {
                DependencyServiceLocator.Current.SetDefaultService<ISqlEntityDataMapper<TEntity, TKey>>(CreateMapper());
            }
            
            return new SqlEntityDataRepository<TEntity, TKey>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static DynamicSqlEntityDataRepository<TEntity, TKey> CreateMixedRepository()
        {
            if (!DependencyServiceLocator.Current.IsDefaultServiceRegistered<ISqlEntityDataMapper<TEntity, TKey>>())
            {
                DependencyServiceLocator.Current.SetDefaultService<ISqlEntityDataMapper<TEntity, TKey>>(CreateMapper());
            }

            return new DynamicSqlEntityDataRepository<TEntity, TKey>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ignoreToProperties"></param>
        /// <returns></returns>
        public static ISqlEntityDataMapper<TEntity, TKey> CreateMapper(IList<string> ignoreToProperties)
        {
            return CreateMapper(ignoreToProperties, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ignoreToProperties"></param>
        /// <param name="ignoreFromProperties"></param>
        /// <returns></returns>
        public static ISqlEntityDataMapper<TEntity, TKey> CreateMapper(IList<string> ignoreToProperties, IList<string> ignoreFromProperties)
        {
            return new BaseSqlEntityDataMapper<TEntity, TKey>(ignoreToProperties, ignoreFromProperties);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        internal static ISqlEntityDataMapper<TEntity, TKey> CreateMapper()
        {
            return new BaseSqlEntityDataMapper<TEntity, TKey>();
        }
    }
}
